/**
 * 
 */
package com.template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.template.utils.TStringUtils;

/**   
*    
* 项目名称：admin   
* 类名称：TMyBatisMapper   
* 类描述：   
* 创建人：henry   
* 创建时间：Jul 5, 2019 10:58:47 AM   
* @version        
*/
public class TMyBatisMapper {
	
	
	public static void createXMLFile(String tableName, String packageName,List<TableBean>columnList,List<TableBean>priList, List<TableBean>noPriList, String folderName, String path) {
		String className = TStringUtils.tableName2ClassName(tableName);
		
		String str = "";
        str +="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
        str +="<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r\n";
        str +="<mapper namespace=\""+packageName+".dao."+className+"Dao\" >\r\n";
        str +="  	<select id=\"list\"  resultType=\"pm\">\r\n";
        str +="		   SELECT\r\n";
		for (int i = 0; i < columnList.size(); i++) {
		str += "            "+columnList.get(i).getColumnName()+ (i==columnList.size()-1?"\r\n":",\r\n");
		}
		str +="		   from\r\n";
		str +="             "+tableName+"\r\n";
		str +="     </select>\r\n";
		str +="	    <insert id=\"add"+className+"\" parameterType=\"pm\">\r\n";
		str +="         insert into "+tableName+"(\r\n";
		for (int i = 0; i < noPriList.size(); i++) {
		str += "                   "+noPriList.get(i).getColumnName()+ (i==noPriList.size()-1?"\r\n":",\r\n");
		}
		str +="             )values(\r\n";
		for (int i = 0; i < noPriList.size(); i++) {
		str += "                  #{"+noPriList.get(i).getColumnName()+ (i==noPriList.size()-1?"}\r\n":"},\r\n");
		}
		str +="		      )\r\n";
		str +="		 </insert>\r\n";
		str +="      <update id=\"edit"+className+"\" parameterType=\"pm\">\r\n";
		str +="          update "+tableName+" set\r\n";
		for (int i = 0; i < priList.size(); i++) {
		str += "          "+priList.get(i).getColumnName()+"=#{"+priList.get(i).getColumnName()+"}"+ (i==priList.size()-1?"\r\n":",\r\n");
		}
		for (int i = 0; i < noPriList.size(); i++) {
		str += "          <if test=\""+noPriList.get(i).getColumnName()+" != null and "+noPriList.get(i).getColumnName()+" != ''\">\r\n";
		str += "          ,"+noPriList.get(i).getColumnName()+"=#{"+noPriList.get(i).getColumnName()+"}\r\n";
		str += "          </if>\r\n";
		}
		str += "          where 1=1 ";
		for (int i = 0; i < priList.size(); i++) {
		str += "and "+priList.get(i).getColumnName()+"=#{"+priList.get(i).getColumnName()+"}"+(i==priList.size()-1?"\r\n":" ");
		}
		str += "     </update>\r\n";
		str += "     <delete id=\"del"+className+"\" parameterType=\"pm\">\r\n";
		str += "            delete from "+tableName+" where ";
		for (int i = 0; i < priList.size(); i++) {
			str += (i==0?"":"and ")+priList.get(i).getColumnName()+"=#{"+priList.get(i).getColumnName()+"}"+(i==priList.size()-1?"\r\n":" ");
		}
		str += "     </delete>\r\n";
		str += "</mapper>\r\n";
		try {
			String allPath = path+"\\"+className+"Mapper.xml";
			OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(allPath),"UTF-8");
			output.write(str);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
